<?php use_helper('Date') ?><?php use_helper('I18N') ?><?php $langIndex = sfConfig::get("sf_lang_index")?>
var baseAPI = 'http://tmd.local/index.php/TMDAPI/';
var lang = 'Th';
var forecastAPILink = new Array(
        'dailyForecast',
        'sevenDaysForecast',
        'threeMonthForecast',
        'weeklySummaryForecat',
        'monthlySummaryForecat'
);

function applySevenDaysForecastByProvince( province, provinceName ) {
    var url = baseAPI + 'inputSevenDaysForecastByProvince';
    var thaiFormat = new Array('<?php echo __("Sunday")?>','<?php echo __("Monday")?>','<?php echo __("Tuesday")?>','<?php echo __("Wednesday")?>','<?php echo __("Thursday")?>','<?php echo __("Friday")?>','<?php echo __("Saturday")?>');
    
    if( province == "NULL" ) {
        return;
    }

    $.post( url, { provinceId: province }, function(data) {
        var list = $.parseJSON(data);
        var boxProvince = $('#box-weather .h-txt-white');
        var boxDay = $('.box-day .txt-head');
        var boxTemp = $('.box-day .txt-white');
        
        $(boxProvince).empty().append( "<?php echo __('7-day Forecast') ?>" + provinceName );
        for( var l in list ) {
            var arr = list[l].DttimeForecast.substring(0, 11).split('-')
            var date = new Date(arr[0], arr[1], arr[2]);
            
            var tempIn = list[l].ItemperatureMin;
            var tempMax = list[l].ItemperatureMax;
            var weatherType = list[l].IweatherType;
            var dateLabel = '';
            if(l == 0) {
                dateLabel = "<?php echo __('Today')?>";
            }
            else {
                dateLabel = thaiFormat[date.getDay()];
            }
            
            $(boxProvince).empty().append( provinceName );
            $(boxDay[l]).empty().append( dateLabel );
            $(boxTemp[l]).empty().append( tempIn + '&#8451; | ' + tempMax +'&#8451' );
        }
    });
}
    

function applyGeographicRegions() {
    
    //alert( "<?php echo $sf_params->get("regionId")?>" );
    
	$.post( baseAPI + 'listGeographicRegions', function( data ) {
		var list = $.parseJSON(data);
		var selected = 0;
		var idx = -1;
		
		Ws.Combo.region.remove(100);
		Ws.Combo.region.insert({value: "NULL", text: "<?php echo __("Select Region")?>", disabled: true});
		
		for( var i in list ) {
			var regionId = list[i].IgeographicRegion;
			if( regionId != 0 && regionId != 7 ) { //Excepted thailand and metropolitant
			    Ws.Combo.region.insert( {value: regionId, text: list[i].SregionName<?php echo $langIndex[$sf_user->getCulture()]?>} );
		    }
			
			if( selected == 0 && regionId == <?php echo $sf_params->get("regionId")? $sf_params->get("regionId"): 3?> ) {
			    selected = idx + 1;
			}
			
			idx++;
		}

		Ws.Combo.region.select( selected );
	});
}

function applyProvinces( region ) {
	$.post( baseAPI + 'listProvinces', { regionId: region }, function( data ) {
		var list = $.parseJSON(data);
		var idx =  0;
		
		Ws.Combo.province.remove(0, 100);
		Ws.Combo.province.insert({value: "NULL", text: "เลือกจังหวัด", disabled: true});
		var selected = 0;
		for( var i in list ) {
		    var provinceId = list[i].IprovinceId;
		    Ws.Combo.province.insert( {value: list[i].IprovinceId, text: list[i].SnameTh} );
		    
		    if( selected == 0 && provinceId == <?php echo $sf_params->get("provinceId")? $sf_params->get("provinceId"): 37 ?> ) {
		        selected = idx + 1;
		    }
		    
		    idx++;
		}

		Ws.Combo.province.select( selected );
	});
}

function applyForecastMainPanel( forecaseAPI ) {
	
	$.post( baseAPI + forecaseAPI, function(data) {
        var list = $.parseJSON( data );
        var items = new Array();
        
        var fieldName = 'SdescTh';
        
        if( forecaseAPI == 'dailyForecast' ) {
        	fieldName = 'SdescTh'
        }
        else if( forecaseAPI == 'sevenDaysForecast' ) {
        	fieldName = 'SweatherTh'
        }
        else if( forecaseAPI == 'threeMonthForecast' ) {
        }

        for( var i in list ) {
            items[list[i].IgeographicRegion] = eval( 'list[' +i + '].' + fieldName );
        }
        
        $('.itemRegion').each( function( idx, obj ) {
            
            var panel = $('.txt-tabs')[idx];
            var id = $(obj).attr('itemRegionId');
            
            panel.innerHTML = items[id];
        });
    });
}


